#include "deletedialog.h"
#include "ui_deletedialog.h"
class TIME{
public:
    tm time;
};

DeleteDialog::DeleteDialog(QString windowtitle,QSqlQuery query,QWidget *parent, bool flag) :windowtitle(windowtitle),query(query),
    QDialog(parent),
    ui(new Ui::DeleteDialog),flag(flag)
{
    ui->setupUi(this);
    this->setWindowTitle(windowtitle + "删除");
}

DeleteDialog::~DeleteDialog()
{
    delete ui;
}
DeleteDialog::DeleteDialog():
ui(new Ui::DeleteDialog)
{
    ui->setupUi(this);
}

void DeleteDialog::InsertComBox()
{
    query.exec(QString("select  column_name,column_type from information_schema.columns where table_name='%1'").arg(windowtitle.mid(0,2)));
    QStringList list;
    while(query.next())
    {
        list.push_back(query.value(0).toString());
    }
    ui->comboBox->addItems(list);
}
void DeleteDialog::AddComBoxChoice()
{

}

void DeleteDialog::SetConnect()
{
    int price;
    int war_price;
    double cost;
    double war_cost;
    connect(ui->comboBox,&QComboBox::currentTextChanged,[=](const QString &text)
    {
        QStringList list;
        ui->comboBox_2->clear();
        if(this->windowtitle=="车位表")
        {
            query.exec("select * from 车位");
            if(text=="Distance")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Have_car")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Price")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
        }
        else if(this->windowtitle=="停车表")
        {
            query.exec("select * from 停车");

            if(text=="Stop_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Pre_time")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="In_time")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Car_char")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(5).toString();
                }
            }
        }
        else if(this->windowtitle=="开出表")
        {
            query.exec("select * from 开出");

            if(text=="Out_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Out_name")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Out_time")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Car_char")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
        }
        else if(this->windowtitle=="用户表")
        {
            query.exec("select * from 用户");
            if(text=="User_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="User_name")
            {

                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }

        }
        else
        {
            ui->comboBox_2->clear();
            query.exec("select * from 预定");
            if(text=="Pre_id")
            {
                while(query.next())
                {
                    list<<query.value(0).toString();
                }
            }
            else if(text=="Pre_outtime")
            {
                while(query.next())
                {
                    list<<query.value(1).toString();
                }
            }
            else if(text=="Pre_intime")
            {
                while(query.next())
                {
                    list<<query.value(2).toString();
                }
            }
            else if(text=="Phone_num")
            {
                while(query.next())
                {
                    list<<query.value(3).toString();
                }
            }
            else if(text=="Set_id")
            {
                while(query.next())
                {
                    list<<query.value(4).toString();
                }
            }
        }
        ui->comboBox_2->addItems(list);
        query.first();
        query.previous();
    });
    connect(ui->comboBox_2,&QComboBox::currentTextChanged,[=,&cost,&war_cost,&price,&war_price](const QString& text){
        query.exec(QString("select * from %1 where %2='%3'").arg(windowtitle.mid(0,2)).arg(ui->comboBox->currentText()).arg(text));
        QString inText = "";
        ui->textEdit->clear();
        if(!flag)
        {
            while(query.next())
            {
                if(windowtitle=="车位表")
                    inText += "车位ID：" + query.value(0).toString() + "\n"
                            + "是否有车：" + query.value(1).toString()+ '\n'
                            + "离出口距离：" + query.value(2).toString()+ '\n'
                            + "价格￥/h：" + query.value(3).toString()+ '\n'
                            + "违规价格￥/h：" + query.value(4).toString() + '\n';
                else if(windowtitle=="开出表")
                    inText += "开出ID：" + query.value(0).toString() + "\n"
                            + "车位ID：" + query.value(1).toString()+ '\n'
                            + "开出人员：" + query.value(2).toString()+ '\n'
                            + "开出时间：" + query.value(3).toString()+ '\n'
                            + "车牌：" + query.value(4).toString() + '\n';
                else if(windowtitle=="停车表")
                    inText += "停车ID：" + query.value(0).toString() + "\n"
                            + "车位ID：" + query.value(1).toString()+ '\n'
                            + "规定开出时间：" + query.value(2).toString()+ '\n'
                            + "开入时间：" + query.value(3).toString()+ '\n'
                            + "车牌：" + query.value(4).toString() + '\n'
                            + "电话：" + query.value(5).toString() + '\n';
                else if(windowtitle=="预定表")
                    inText += "预定ID：" + query.value(0).toString() + "\n"
                            + "预定开出时间：" + query.value(1).toString()+ '\n'
                            + "预定开入时间：" + query.value(2).toString()+ '\n'
                            + "预留电话：" + query.value(3).toString()+ '\n'
                            + "预定车位：" + query.value(4).toString() + '\n';
                else
                    inText += "用户ID：" + query.value(0).toString() + "\n"
                            + "用户名：" + query.value(1).toString()+ '\n'
                            + "电话：" + query.value(2).toString()+ '\n'
                            + "车牌：" + query.value(3).toString()+ '\n';
                inText += "--------------------------\n" ;
            }
            ui->textEdit->setText(inText);
        }
        else
        {
            while(query.next())
            {
                inText += "<p id='1'>停车ID：" + query.value(0).toString() + "</p>"
                        + "<p id='2'>车位ID：" + query.value(1).toString()+ "</p>"
                        + "<p id='3'>规定开出时间：" + query.value(2).toString()+ "</p>"
                        + "<p id='4'>开入时间：" + query.value(3).toString()+ "</p>"
                        + "<p id='5'>车牌：" + query.value(4).toString() + "</p>"
                        + "<p id='6'>电话：" + query.value(5).toString() + "</p>";

                QString in_time = query.value(3).toString();
                QString out_time = query.value(2).toString();
                //实际开入时间
                tm* in = new tm();
                QStringList y_m_d = in_time.split('-');
                QStringList d = y_m_d[2].split('T');
                QStringList h_m_s = d[1].split(':');
                in->tm_year = y_m_d[0].toInt() - 1900;
                in->tm_mon = y_m_d[1].toInt() - 1;
                in->tm_mday = d[0].toInt();
                in->tm_hour = h_m_s[0].toInt();
                in->tm_min = h_m_s[1].toInt();
                in->tm_sec = h_m_s[2].toInt();
                in->tm_isdst = 0;
//                qDebug()<<"in:"<<in->tm_year<<in->tm_mon<<in->tm_mday;
                time_t in_t = mktime(in);
                //预定开出时间
                tm* out = new tm();
                y_m_d = out_time.split('-');
                d = y_m_d[2].split('T');
                h_m_s = d[1].split(':');
                out->tm_year = y_m_d[0].toInt() - 1900;
                out->tm_mon = y_m_d[1].toInt() - 1;
                out->tm_mday = d[0].toInt();
                out->tm_hour = h_m_s[0].toInt();
                out->tm_min = h_m_s[1].toInt();
                out->tm_sec = h_m_s[2].toInt();
                out->tm_isdst = 0;
//                qDebug()<<"out:"<<out->tm_year<<out->tm_mon<<out->tm_mday;
                time_t out_t = mktime(out);
                double pre_costTime = out_t - in_t;
                qDebug()<<"规定时间"<<pre_costTime;
                //点击按钮的时间
                time_t tt = time(NULL);
                tm* now = localtime(&tt);
                time_t now_t = mktime(now);

                int exceed = now_t - out_t;//超出预定开出的时间
                qDebug()<<"超期时间"<<exceed;

                int costTime = now_t - in_t;// 规定时间内开出的时间
                qDebug()<<"规定时间内开出"<<costTime;
                //判断是否超过预定时间
                QSqlQuery q;

                //查询车位表中的价格
                q.exec(QString("select Price,War_price from 车位 where Set_id=%1").arg(query.value(1).toString()));
                while(q.next()){
                    price = q.value(0).toInt();
                    war_price = q.value(1).toInt();
                }
                if(exceed>0){//超时，前面预定时间采用预定的价格，后面的超时价格采用惩罚价格
                    war_cost = exceed/(60*60)*war_price;
                    cost = pre_costTime/(60*60)*price;
                    inText += QString("<p id='7' style='color:red;'>花费：%1").arg(cost) + "</p>";
                    inText +="<p id='8' style='color:red'>" + QString("超期花费：%1").arg(war_cost)  + "</p>";
                }
                 else{
                     cost = costTime/(60*60)*price;
                     inText += QString("<p id='7' style='color:red;'>花费：%1").arg(cost) + "</p>";
                 }
                 inText += "<p id ='9'>--------------------------</p>";


            }
            QTextCursor cursor = ui->textEdit->textCursor();
            cursor.insertHtml(inText);
        }
       ui->textEdit->setReadOnly(true);
    });
    if(!flag)
    {//针对于非root用户对停车表操作
        connect(ui->pushButton,&QPushButton::clicked,[=](){
        QMessageBox msgBox;
        msgBox.setText("确认删除");
        msgBox.setInformativeText(QString("确定要删除%1为%2的数据吗?").arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText()));
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::No);
        int ret = msgBox.exec();
        if(ret==QMessageBox::Yes)
        {
           query.prepare(QString("delete from %1 where %2='%3'").arg(windowtitle.mid(0,2)).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText()));
           bool ok = query.exec();
           if(ok)
           {
               QMessageBox::information(this,"提示","删除成功",QMessageBox::Ok);
           }
           else
           {
               QSqlError error = query.lastError();
               QMessageBox::warning(this,"错误",error.text().toUtf8().data(),QMessageBox::Ok);
           }
        }

        });
    }
    else
    {
        connect(ui->pushButton,&QPushButton::clicked,[=](){
            qDebug()<<"cost"<<cost;
            qDebug()<<"war_cost"<<war_cost;

            QMessageBox msgBox;
            msgBox.setText("确认开出");
            msgBox.setInformativeText(QString("确定要开出%1为%2的车吗?并支付%3的费用").arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText()).arg(double(cost+war_cost)));
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            msgBox.setDefaultButton(QMessageBox::No);
            int ret = msgBox.exec();
            if(ret==QMessageBox::Yes)
            {
               query.prepare(QString("delete from %1 where %2='%3'").arg(windowtitle.mid(0,2)).arg(ui->comboBox->currentText()).arg(ui->comboBox_2->currentText()));
               bool ok = query.exec();
               if(ok)
               {
                   QMessageBox::information(this,"提示","开出成功",QMessageBox::Ok);
               }
               else
               {
                   QSqlError error = query.lastError();
                   QMessageBox::warning(this,"错误",error.text().toUtf8().data(),QMessageBox::Ok);
               }
            }
            else
            {

            }

        });
    }
}
